package com.baizhi.controller;

import com.baizhi.entity.Brand;
import com.baizhi.entity.Result;
import com.baizhi.service.BrandService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<Brand> findAll(){
        return brandService.findAll();
    }

    @RequestMapping("findById")
    @ResponseBody
    public Brand findById(String id){
        return brandService.findById(id);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(Brand brand){
        Result result = null;
        try{
            brandService.update(brand);
            result = new Result();
            result.setStatus(true);
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("更新失败");
        }
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(MultipartFile brandimage, HttpServletRequest request,String name){
        Result result = null;
        try{
            Brand brand = new Brand();
            String realPath = request.getSession().getServletContext().getRealPath("/back/image/brand");
            String uuid = UUID.randomUUID().toString();
            brand.setId(uuid);
            brand.setName(name);
            String extension = FilenameUtils.getExtension(brandimage.getOriginalFilename());
            String urlname=uuid+"."+extension;
            brandimage.transferTo(new File(realPath,urlname));
            String path = "/back/image/brand/"+urlname;
            brand.setPath(path);
            brandService.save(brand);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result detete(String id,HttpServletRequest request){
        Result result = null;
        try{
            Brand brand = brandService.findById(id);
            String realPath = request.getSession().getServletContext().getRealPath(brand.getPath());
            File file = new File(realPath);
            file.delete();
            brandService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("删除失败");
        }
        return result;
    }
}
